<template>
  <view class="container">
    <view class="header">
      <text class="title">添加设备</text>
    </view>
    
    <view class="form-container">
      <view class="form-item">
        <text class="form-label">设备名称</text>
        <input class="form-input" v-model="deviceForm.name" placeholder="请输入设备名称" />
      </view>
      
      <view class="form-item">
        <text class="form-label">设备类型</text>
        <picker class="form-picker" @change="onTypeChange" :value="typeIndex" :range="deviceTypes">
          <view class="picker-view">
            {{ deviceTypes[typeIndex] || '请选择设备类型' }}
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <text class="form-label">所属单位</text>
        <input class="form-input" v-model="deviceForm.unit" placeholder="请输入所属单位" />
      </view>
      
      <view class="form-item">
        <text class="form-label">区域</text>
        <picker class="form-picker" @change="onControlTypeChange" :value="controlTypeIndex" :range="controlTypes">
          <view class="picker-view">
            {{ controlTypes[controlTypeIndex] || '请选择控制类型' }}
          </view>
        </picker>
      </view>
      
      <view class="btn-container">
        <button class="submit-btn" @click="submitForm">提交</button>
        <button class="cancel-btn" @click="goBack">取消</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      deviceForm: {
        name: '',
        type: '',
        unit: '',
        controlType: ''
      },
      typeIndex: 0,
      controlTypeIndex: 0,
      deviceTypes: ['类型1', '类型2', '类型3', '类型4', '类型5'],
      controlTypes: ['区域A', '区域B', '区域C', '区域D', '区域E']
    };
  },
  methods: {
    onTypeChange(e) {
      this.typeIndex = e.detail.value;
      this.deviceForm.type = this.deviceTypes[this.typeIndex];
    },
    
    onControlTypeChange(e) {
      this.controlTypeIndex = e.detail.value;
      this.deviceForm.controlType = this.controlTypes[this.controlTypeIndex];
    },
    
    submitForm() {
      // 表单验证
      if (!this.deviceForm.name) {
        uni.showToast({
          title: '请输入设备名称',
          icon: 'none'
        });
        return;
      }
      
      if (!this.deviceForm.type) {
        uni.showToast({
          title: '请选择设备类型',
          icon: 'none'
        });
        return;
      }
      
      if (!this.deviceForm.unit) {
        uni.showToast({
          title: '请输入所属单位',
          icon: 'none'
        });
        return;
      }
      
      if (!this.deviceForm.controlType) {
        uni.showToast({
          title: '请选择控制类型',
          icon: 'none'
        });
        return;
      }
      
      // 这里应该调用API保存设备信息
      console.log('提交设备信息:', this.deviceForm);
      
      // 模拟API调用成功
      uni.showLoading({
        title: '正在添加设备...'
      });
      
      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({
          title: '设备添加成功',
          icon: 'success'
        });
        
        // 添加成功后返回列表页
        setTimeout(() => {
          uni.navigateBack();
        }, 1500);
      }, 2000);
    },
    
    goBack() {
      uni.navigateBack();
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.form-container {
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-item {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 16px;
  color: #333;
  margin-bottom: 8px;
}

.form-input {
  /*width: 100%;*/
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 10px;
  font-size: 15px;
}

.form-picker {
  width: 100%;
}

.picker-view {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 10px;
  font-size: 15px;
}

.btn-container {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

.submit-btn, .cancel-btn {
  width: 45%;
  height: 44px;
  line-height: 44px;
  text-align: center;
  border-radius: 22px;
  font-size: 16px;
}

.submit-btn {
  background-color: #007aff;
  color: #fff;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #333;
  border: 1px solid #ddd;
}
</style> 